
#计算器

class Calculator:
    def __init__(self):
        self.stack = []

    def evaluate(self, expression):
        tokens = expression.split()  # 将表达式解析成操作数和运算符的列表
        for token in tokens:
            if token.isdigit():  # 如果是数字，压入栈中
                self.stack.append(int(token))
            else:
                # 遇到运算符，从栈中弹出对应数量的操作数，进行运算
                operand2 = self.stack.pop()
                operand1 = self.stack.pop()
                if token == '+':
                    self.stack.append(operand1 + operand2)
                elif token == '-':
                    self.stack.append(operand1 - operand2)
                elif token == '*':
                    self.stack.append(operand1 * operand2)
                elif token == '/':
                    self.stack.append(operand1 / operand2)
        return self.stack[-1]  # 返回栈顶元素作为计算结果


# 测试计算器
calculator = Calculator()
result = calculator.evaluate("3 4 + 5 *")
print("计算结果:", result)  # 应该输出 35